package com.aysafety.iotdataplatformserver.configurer;

import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Order(2)
@ConfigurationProperties("iotnettyserver")
public class NettyServer implements CommandLineRunner {
    private Logger logger = LoggerFactory.getLogger(NettyServer.class);

    private int port;

    private int backLogParam;

    private boolean keepAlive;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getBackLogParam() {
        return backLogParam;
    }

    public void setBackLogParam(int backLogParam) {
        this.backLogParam = backLogParam;
    }

    public boolean isKeepAlive() {
        return keepAlive;
    }

    public void setKeepAlive(boolean keepAlive) {
        this.keepAlive = keepAlive;
    }

    @Autowired
    IotDataPlatformServer iotDataPlatformServer;

    @Autowired
    HBaseConfigurer hBaseConfigurer;

    @Override public void run(String... args) {
        try {
            HBaseDriver.setZKConfig(hBaseConfigurer.getZkHost(), hBaseConfigurer.getZkPort());
            HBaseDriver.connection();
            logger.info("iotDataPlatformServer启动中...");
            iotDataPlatformServer.setParams(this.getBackLogParam(), this.isKeepAlive());
            iotDataPlatformServer.run(this.getPort());
        } catch (Exception e) {
            logger.error("iotDataPlatformServer异常了:", e);
        } finally {
            try {
                HBaseDriver.close();
            } catch (IOException e) {
                logger.error("关闭hbase异常:", e);
            }
        }
    }
}
